
<template>
  <div class="flex flex-col h-full">
    <el-form :inline="true" :model="selectProps" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="selectProps.name" placeholder="请输入职位名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSelect">搜索</el-button>
        <el-button type="danger" @click="deleteDeps">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table class="flex-1 " :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection"  width="55" />
      <el-table-column prop="name" label="职位名称" align="center" />
      <el-table-column prop="remark" label="详细信息" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="info" size="small" @click="getDepInfo(scope)">编辑</el-button>
          <el-button type="danger" size="small" @click="deleteDep([scope.row.id])">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination class="mt-2" v-model:current-page="tableProps.currentPage" v-model:page-size="tableProps.pageSize"
      :page-sizes="[15, 30]" layout="total, sizes, prev, pager, next, jumper" :total="tableProps.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <el-dialog v-model="depInfoVisible" title="信息修改" width="550">
    <el-form ref="formRef" :inline="true" :model="depInfo" class="demo-form-inline" :rules="rules">
      <el-form-item prop="name" label="职位名称：">
        <el-input v-model="depInfo.name" clearable />
      </el-form-item>
      <el-form-item prop="remark" label="详细信息：">
        <el-input v-model="depInfo.remark" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="depInfoVisible = false">取消</el-button>
        <el-button type="primary" @click="handleDepInfoChange(formRef)">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ApiDelete, ApiPost, ApiPut } from '@/utils/Axios';
import { ElMessageBox, ElNotification, type FormInstance } from 'element-plus';
import { onBeforeMount, reactive, ref, watch } from 'vue'
const tableProps = ref({
  currentPage: 1,
  pageSize: 15,
  total: 30,
})
const selectProps = reactive({
  name: "",
})
const tableData = ref([])
const multipleSelection = ref([])
const depInfo = ref({
  id: null,
  name: "",
  remark: "",
})
const depInfoVisible = ref(false)
const formRef = ref<FormInstance>()

onBeforeMount(() => {
  getTableData(tableProps.value.currentPage, tableProps.value.pageSize)
})

watch(tableProps, (newVal, oldVal) => {
  getTableData(newVal.currentPage, newVal.pageSize)
})


const getTableData = (currentPage, pageSize) => {
  ApiPost("/jobInf/page", {
    page: Number(currentPage),
    pageSize: Number(pageSize),
    name: selectProps.name ? selectProps.name : null,
  }).then(res => {
    if (res.code === 200) {
      tableData.value = res.data.records
      tableProps.value.total = res.data.total
    } else {
      console.log("错误=>", res.msg);
    }
  }).catch(err => {
    console.log("错误=>", err);
  })
}

const onSelect = () => {
  tableProps.value.currentPage = 1
  getTableData(tableProps.value.currentPage, tableProps.value.pageSize)
}

const deleteDep = (depList: number[]) => {
  ElMessageBox({
    title: "警告",
    type: "warning",
    message: "确认删除该职位吗？",
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    showCancelButton: true,
  }).then(() => {
    ApiDelete("/jobInf/delete", { ids: depList.join(",") }).then(res => {
      if(res.code === 200){
        getTableData(tableProps.value.currentPage, tableProps.value.pageSize)
        ElNotification({
          title: "删除成功",
          type: "success",
          showClose: false,
          duration: 2000,
        })
      }else{
        ElNotification({
          title: "删除失败",
          type: "error",
          message: res.msg,
          showClose: false,
          duration: 2000,
        })
      }
    }).catch((err)=>{
      console.log("删除失败")
    })
  }).catch(()=>{
    return
  })
}

const deleteDeps = () =>{
  if(multipleSelection.value.length === 0){
    return
  }
  const depList = multipleSelection.value.map(item=>item.id)
  deleteDep(depList)
}

const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const getDepInfo = (scope) => {
  depInfo.value = { ...scope.row }
  depInfoVisible.value = true
}

const validateDepname = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('职位名称不能为空'))
  } else {
    callback()
  }
}

const validateDepremark = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('详细信息不能为空'))
  } else {
    callback()
  }
}
const rules = reactive({
  name: [{ validator: validateDepname, trigger: 'blur' }],
  remark: [{ validator: validateDepremark, trigger: 'blur' }],
})

const handleDepInfoChange = (formEl: FormInstance | undefined) => {
  formEl.validate(vail => {
    if (vail && depInfo.value.id) {
      ElMessageBox({
        title: "提醒",
        type: "warning",
        message: "确认修改该职位信息吗？",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showCancelButton: true,
      }).then(() => {
        const { id, name, remark } = depInfo.value
        ApiPut("/jobInf/update", { id, name, remark }).then(res => {
          console.log(res)
          if (res.code === 200) {
            depInfoVisible.value = false
            getTableData(tableProps.value.currentPage, tableProps.value.pageSize)
            ElNotification({
              title: "修改成功",
              type: "success",
              showClose: false,
              duration: 2000,
            })
          }
        })
      }).catch(() => {
        return
      })
    }
  })

}

const handleSizeChange = (val) => {
  tableProps.value = { ...tableProps.value, pageSize: val };
};
const handleCurrentChange = (val) => {
  tableProps.value = { ...tableProps.value, currentPage: val };
};
</script>